﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using hachiModel;

public partial class ProductDetail : System.Web.UI.Page
{
    List<Item> li = new List<Item>();
    Hachi.Controllers.ProductsController pctr = new Hachi.Controllers.ProductsController();
    string productImageThumb = "upload/Products/thumb/{0}.jpg";
    string productImageSmall = "upload/Products/small/{0}.jpg";
    string productImageLarge = "upload/Products/large/{0}.jpg";

    //string productImageThumb = "http://hachihachi.com.vn/Portals/0/Products/small/{0}.jpg";
    //string productImageSmall = "http://hachihachi.com.vn/Portals/0/Products/{0}.jpg";
    //string productImageLarge = "http://hachihachi.com.vn/Portals/0/Products/{0}.jpg";
    //http://113.161.89.207:82/


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string cck = hachiCookies.getCookieValue("recentview");
            try
            {
                if (Session["recentview"] != null)
                    li = (List<Item>)(Session["recentview"]);
                else if (!string.IsNullOrEmpty(cck))
                {
                    string[] i = cck.Split('-');
                    li = new List<Item>();
                    for (int j = 1; j < i.Count(); j++)
                    {
                        string[] d = i[j - 1].Split(':');
                        Item it = new Item();
                        it.ItemID = int.Parse(d[0]);
                        it.Quantity = int.Parse(d[1]);
                        li.Add(it);
                    }

                }
                if (Request.QueryString["pid"] != null)
                {
                    int id = int.Parse(Request.QueryString["pid"]);
                    Item i = li.Where(d => d.ItemID == id).FirstOrDefault();
                    if (i == null)
                    {
                        i = new Item(); i.ItemID = id; li.Insert(0,i);
                        Session["recentview"] = li;
                        string ck = "";
                        foreach (var item in li)
                            ck += item.ItemID + ":" + item.Quantity + "-";
                        hachiCookies.setCookieValue("recentview", ck);
                    }
                    loadProductInfo(id);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        
        
    }

    private void loadProductInfo(int id)
    {
        string lang = Session["Lang"] == null ? hachiCookies.getCookieValue("Lang") : Session["Lang"].ToString();
        hachiModel.PR_Product p = pctr.getProduct(id);
        long hits = pctr.HitProduct(id);
        if (p != null)
        {
            #region load detail
            hachiModel.PR_ProductDetail pd = pctr.getProductDetailToShow(p, lang);
            lblMeta.Text = @"<meta itemprop='name' content='" + pd.ProductName + "'><meta itemprop='description' content='" 
                + pd.Uses + " hits: "+ hits.ToString() + "'><meta itemprop='image' content='"+string.Format(productImageSmall,p.DisplayCode)+"'>";
            lblMeta.Text += "<script>var cCategoryID = " + p.CategoryID + "; var cProductID = " + p.ProductID + "; var cProductQuantity = " + p.Quantity + ";</script>";
            ltrItemName.Text = lblPageTitle.Text = pd.ProductName;

            ltrProductDetail.Text = string.Format("<span id='productinfodetail_code' title='hit: " + hits + "'></span>{0}<br />", p.ProductCode); ;
            ltrProductDetail.Text += string.Format("<span id='productinfodetail_Origin'></span>{0}<br />",pd.Origin);
            ltrProductDetail.Text += string.Format("<span id='productinfodetail_Material'></span>{0}<br />",pd.Material);
            ltrProductDetail.Text += string.Format("<span id='productinfodetail_Size'></span>{0}<br />",pd.Size);
            ltrProductDetail.Text += string.Format("<span id='lblproductinfodetail_Quantity'></span>{0}<br />", p.Quantity);
            string price = p.IsSaleOff? String.Format("{0:#,#.# vnđ}", p.PriceSaleOff):String.Format("{0:#,#.# vnđ}", p.Price);
            ltrPrice.Text = string.Format("<label id='productinfodetail_Price'></label><b class='price'>{0}</b>",price);
            if (!string.IsNullOrEmpty(pd.Uses) && !string.IsNullOrEmpty(pd.Uses.Trim()))
                ltrProductGuide.Text = "<br /><h3><b class='iconQuestion'></b><b id='lblProductGuide'></b></h3>"+pd.Uses;
            if (!string.IsNullOrEmpty(pd.Summary))
                ltrProductIntro.Text = "<br /><h3><b class='iconQuestion'></b><b id='lblProductIntro'></b></h3>"+pd.Summary;

            string snew = "";
            foreach (NW_NewsDetail item in pctr.GetProductNews(p,lang))
                snew = string.Format("<a href='{0}'>{1}</a>", item.Title, item.Alias) + string.Format("<span>{0}</span>", item.Summary);
            
            if (!string.IsNullOrEmpty(snew))
                ltrProductNews.Text = "<br /><h3><b class='iconQuestion'></b><b id='lblProductNews'></b></h3>" + snew;
            

           
            #endregion
            #region load category info
            Hachi.Controllers.CategoriesController cctr = new Hachi.Controllers.CategoriesController();
            PR_Category c = cctr.getCategory(p.CategoryID, lang);
            PR_CategoryDetail cd = cctr.getCategoryDetail(c.CategoryID, lang);
            if (cd==null)
                cd = new PR_CategoryDetail();
            ltrSameProduct.Text = "<a href='"+cd.Alias+"'><div class='sanphamtuongtuheader' id='lblSameProducts'></div></a>";
            #endregion
            #region load images
            ltrDefaultImage.Text = @"<a href='" + string.Format(productImageLarge, p.DisplayCode) + "' class='jqzoom' rel='gal1' title='" + pd.ProductName +
                            "'><img src='" + string.Format(productImageSmall, p.DisplayCode) + "' ></a>";
            ltrProductThumblist.Text = "<a class='zoomThumbActive' href='javascript:void(0);' rel=\"{gallery: 'gal1', smallimage: '"
                + string.Format(productImageSmall, p.DisplayCode) + "',largeimage: '" + string.Format(productImageLarge, p.DisplayCode)
                + "'}\"><img src='" + string.Format(productImageThumb, p.DisplayCode) + "' /></a>";
            int imgcount = 0;
            foreach (PR_ProductImages item in pctr.GetProductImageByProduct(p))
            {
                ltrProductThumblist.Text += "<a href='javascript:void(0);' rel=\"{gallery: 'gal1', smallimage: '" +
                    string.Format(productImageSmall, item.Src) + "',largeimage: '" +
                    string.Format(productImageLarge, item.Src) + "'}\"><img src='" +
                    string.Format(productImageThumb, item.Src) + "' /></a>";
                imgcount++;
            }

            ltrProductThumblist.Text += "<a href='javascript:void(0);' rel=\"{gallery: 'gal1', smallimage: 'upload/Products/small/4903294005842.jpg',largeimage: 'upload/Products/large/4903294005842.jpg'}\">"+
                "<img src='upload/Products/thumb/4903294005842.jpg' /></a><a href='javascript:void(0);'"+
                "rel=\"{gallery: 'gal1', smallimage: 'upload/Products/small/4976790764216.jpg',largeimage: 'upload/Products/large/4976790764216.jpg'}\"><img src='upload/Products/thumb/4976790764216.jpg' /></a>";
            imgcount += 2;
            if (imgcount<4)
            {
                ltrProductThumblist.Text =
                "<div style='width:307px;'><div style='float: right; position: relative; left: -50%; text-align: left;'><div style='position: relative;left: 50%;'>" +
                    ltrProductThumblist.Text +
                    "</div></div></div>";
            }

            #endregion
            #region getRate
            int Sum = p.Vote1 * 1 + p.Vote2 * 2 + p.Vote3 * 3 + p.Vote4 * 4 + p.Vote5 * 5;
            int Count = p.Vote1 + p.Vote2 + p.Vote3 + p.Vote4 + p.Vote5;
            double Avg = (Sum * 1.0) / (Count);
            voteProduct.Star = Avg;
            #endregion
        }
        
   } 
}